package com.spn.blog.article.component.rocketmq;

import com.spn.blog.article.service.ESservice;
import com.spn.blog.common.dao.pojo.Article;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
@RocketMQMessageListener(consumerGroup = R.ROCKETMQ_GROUP_ES_Article_NAME, topic = R.ROCKETMQ_TOPIC_ES_Article_NAME
        , consumeMode = ConsumeMode.ORDERLY)
public class ArticleToESConsumer implements RocketMQListener<Article> {


    @Autowired
    private ESservice eSservice;

    @Override
    public void onMessage(Article article) {
        // 防止重复消费未编写
        // 防止消息丢失未编写
        // 接口幂等问题

        // 提交到ES服务器
        boolean bool = false;
        try {
            log.info("rocketMQ消费者正在消费信息：" + article);
            bool = eSservice.addOrUpdates(article.getId(), article);
            if (bool) log.info("rocketMQ消费成功！");
            else log.info("rocketMQ消费失败！");
        } catch (IOException e) {
            log.info("rocketMQ消费发生了异常！");
        }
    }
}
